<?php

namespace App\Models;

use App\Models\Traits\GoldProductTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Traits\GoldProductCacheTrait;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * App\Models\GoldProduct
 *
 * @property int $id
 * @property int $gold_seed_id
 * @property int $gold_round_id
 * @property \Illuminate\Support\Carbon|null $disabled_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\GoldRecord[] $goldRecords
 * @property-read int|null $gold_records_count
 * @property-read \App\Models\GoldRound|null $goldRound
 * @property-read \App\Models\GoldSeed|null $goldSeed
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\GoldSplit[] $goldSplits
 * @property-read int|null $gold_splits_count
 * @method static \Database\Factories\GoldProductFactory factory(...$parameters)
 * @method static Builder|GoldProduct index($roundId = null)
 * @method static Builder|GoldProduct newModelQuery()
 * @method static Builder|GoldProduct newQuery()
 * @method static Builder|GoldProduct query()
 * @method static Builder|GoldProduct whereCreatedAt($value)
 * @method static Builder|GoldProduct whereDisabledAt($value)
 * @method static Builder|GoldProduct whereGoldRoundId($value)
 * @method static Builder|GoldProduct whereGoldSeedId($value)
 * @method static Builder|GoldProduct whereId($value)
 * @method static Builder|GoldProduct whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \App\Models\GoldEnd|null $goldEnd
 * @property \Illuminate\Support\Carbon|null $started_at
 * @method static Builder|GoldProduct whereStartedAt($value)
 */
class GoldProduct extends Model
{
    use HasFactory,
	    GoldProductTrait,
	    GoldProductCacheTrait;

	protected $guarded = ['id'];

	protected $dates = [
        'started_at',
		'disabled_at',
	];

	public function scopeIndex(Builder $builder, $roundId = null): Builder
	{
		$builder = $builder->whereNull('disabled_at')->whereDate('started_at', '<=', Carbon::today());
		return $roundId ? $builder->where('gold_round_id', $roundId) : $builder;
	}

	public function goldSeed(): BelongsTo
	{
		return $this->belongsTo(GoldSeed::class);
	}

	public function goldRound(): BelongsTo
	{
		return $this->belongsTo(GoldRound::class);
	}

	public function goldRecords(): HasMany
	{
		return $this->hasMany(GoldRecord::class);
	}

	public function goldSplits(): HasMany
	{
		return $this->hasMany(GoldSplit::class);
	}

    public function goldEnd(): HasOne
    {
        return $this->hasOne(GoldEnd::class);
    }
}
